package com.systop.ltbook.mainpage;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.systop.core.webapp.struts2.action.DefaultCrudAction;
import com.systop.ltbook.BookConstants;
import com.systop.ltbook.book.model.Book;
import com.systop.ltbook.book.press.model.Press;
import com.systop.ltbook.book.press.service.PressManager;
import com.systop.ltbook.book.service.BookManager;
import com.systop.ltbook.book.subject.service.SubjectManager;

/**
 * 首页更多图书的struts2 action。
 * 
 * @author DU
 */
@SuppressWarnings({"serial","unchecked"})
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class MainBookAction extends DefaultCrudAction<Book, BookManager> {
	
	/** pageCompany 图书列表当前页索引 ,初始化为0 */
	private Integer currentPage = 0;
	
	/** pageSize 页容量 */
	private Integer pgSize = BookConstants.MORE_BOOKS_PAGE_ZISE;
	
	/** pageCount 数据页总数 */
	private Integer pgCount = 0;
	
	/** beginIndex 起始索引 */
	private Integer beginIndex = 0;
		
	@Autowired
	private PressManager pressManager;
	
	@Autowired
	private SubjectManager subjectManager;
	
	/**
	 * 首页图书
	 */
	@Override
	public String index() {
		booklist = getManager().getBooksByType(bookType, pressId, subjectId, gradeId,
				pgSize, currentPage);
		int bookCount = getManager().getBookCount(bookType, pressId, subjectId, gradeId);
		logger.debug("图书总数：{}", bookCount);
		pgCount = bookCount % pgSize == 0 ? bookCount / pgSize
				: bookCount / pgSize + 1;// 计算总页数
		logger.debug("总页数：{}", pgCount);
		beginIndex = pgSize*currentPage + 1;//计算起始索引	
		
		if (pressId != null) {
			pressName = pressManager.getPressById(pressId).getName();
		}
		if (subjectId != null) {
			subjectName = subjectManager.getSubjectById(subjectId).getName();
		}
		if (StringUtils.isNotBlank(gradeId)) {
			gradeName = BookConstants.GRADE_NAME_MAP.get(gradeId);
		}
		
		return SUCCESS;
	}
	
	/**
	 * 书库显示时列出的所有的出版社
	 */
	public String indexPress() {
		pressMaps = pressManager.getAllGroupByCharName();
		/*subjectList = subjectManager.getAllSubjects();
		primaryMap = BookConstants.PRIMARY_MAP;
		juniorMap = BookConstants.JUNIOR_MAP;
		seniorMap = BookConstants.SENIOR_MAP;*/
		return "indexPress";
	}
	
	/**
	 * 推荐分类, 学科 年级
	 */
	public String recommend() {
		subjectList = subjectManager.getAllSubjects();
		primaryMap = BookConstants.PRIMARY_MAP;
		juniorMap = BookConstants.JUNIOR_MAP;
		seniorMap = BookConstants.SENIOR_MAP;
		return "recommend";
	}
	
	/**
	 * 获得图书查询结果
	 */
	public List getBooklist() {
  	return booklist;
  }
	
	public void setBooklist(List booklist) {
  	this.booklist = booklist;
  }
	
	/** 图书列表分页信息 */
	private List booklist;
	
	/** 图书类型 */
	private String bookType;
	
	/** 出版社ID*/
	private Integer pressId;
	
	/** 出版社名称*/
	private String pressName;
	
	/** 出版社列表 */
	private Map<String, List<Press>> pressMaps;
	
	/** 学科列表 */
	private List subjectList;
	
	/** 小学列表 */
	private Map primaryMap;
	
	/** 初中列表 */
	private Map juniorMap;
	
	/** 高中列表 */
	private Map seniorMap;
	
	/** 学科ID */
	private Integer subjectId;
	
	/** 学科名称 */
	private String subjectName;
	
	/** 年级ID */
	private String gradeId;
	
	/** 年级名称 */
	private String gradeName;
	
	public Integer getBeginIndex() {
  	return beginIndex;
  }

	public void setBeginIndex(Integer beginIndex) {
  	this.beginIndex = beginIndex;
  }

	public String getBookType() {
  	return bookType;
  }

	public void setBookType(String bookType) {
  	this.bookType = bookType;
  }

	public Integer getCurrentPage() {
  	return currentPage;
  }

	public void setCurrentPage(Integer currentPage) {
  	this.currentPage = currentPage;
  }

	public Integer getPgSize() {
  	return pgSize;
  }

	public void setPgSize(Integer pgSize) {
  	this.pgSize = pgSize;
  }

	public Integer getPgCount() {
  	return pgCount;
  }

	public void setPgCount(Integer pgCount) {
  	this.pgCount = pgCount;
  }

	public Map<String, List<Press>>  getPressMaps() {
  	return pressMaps;
  }

	public void setPressMaps(Map<String, List<Press>>  pressMaps) {
  	this.pressMaps = pressMaps;
  }

	public Integer getPressId() {
  	return pressId;
  }

	public void setPressId(Integer pressId) {
  	this.pressId = pressId;
  }

	public String getPressName() {
  	return pressName;
  }

	public void setPressName(String pressName) {
  	this.pressName = pressName;
  }

	public List getSubjectList() {
  	return subjectList;
  }

	public void setSubjectList(List subjectList) {
  	this.subjectList = subjectList;
  }

	public Map getPrimaryMap() {
  	return primaryMap;
  }

	public void setPrimaryMap(Map primaryMap) {
  	this.primaryMap = primaryMap;
  }

	public Map getJuniorMap() {
  	return juniorMap;
  }

	public void setJuniorMap(Map juniorMap) {
  	this.juniorMap = juniorMap;
  }

	public Map getSeniorMap() {
  	return seniorMap;
  }

	public void setSeniorMap(Map seniorMap) {
  	this.seniorMap = seniorMap;
  }

	public Integer getSubjectId() {
  	return subjectId;
  }

	public void setSubjectId(Integer subjectId) {
  	this.subjectId = subjectId;
  }

	public String getGradeId() {
  	return gradeId;
  }

	public void setGradeId(String gradeId) {
  	this.gradeId = gradeId;
  }

	public String getSubjectName() {
  	return subjectName;
  }

	public void setSubjectName(String subjectName) {
  	this.subjectName = subjectName;
  }

	public String getGradeName() {
  	return gradeName;
  }

	public void setGradeName(String gradeName) {
  	this.gradeName = gradeName;
  }
	
	@SuppressWarnings("unused")
	public String nullPressesList(){
		String hql = "from Press p order by p.id";
		List<Press> presses = getManager().getDao().query(hql);
		String pressIds = "";
		for(Press p : presses){
			hql = "select count(b.id) as count from Book b where b.press.id = ?";
			Long count = (Long) getManager().getDao().findObject(hql, p.getId());
			if (count == 0){
				pressIds += p.getId() + ",";
			}
		}
		System.out.println(pressIds);
		return SUCCESS;
	}
}
